package cn.bonoon.kernel.io;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <pre>
 * 文件保存目录的生成策略
 * </pre>
 * @author jackson
 *
 */
public enum DirectoryStrategy {
	/**
	 * 不需要对文件进行分目录存储
	 */
	NONE {
		@Override
		public String formatPath(String relativePath) {
			return relativePath;
		}
	},
	/**
	 * <pre>
	 * 以年为单位进行存储，如：
	 * /upload/2012
	 * /upload/2013
	 * </pre>
	 */
	YEAR {
		private final SimpleDateFormat YEAR_FORMAT = new SimpleDateFormat("yyyy");
		@Override
		public String formatPath(String relativePath) {
			return formatPath(relativePath, YEAR_FORMAT);
		}
	},
	/**
	 * <pre>
	 * 以月份进行存储，如：
	 * /upload/2012/1
	 * /upload/2013/12
	 * </pre>
	 */
	YEAR_MONTH {
		private final SimpleDateFormat YEAR_MONTH_FORMAT = new SimpleDateFormat("yyyy" + separatorChar + "MM");
		@Override
		public String formatPath(String relativePath) {
			return formatPath(relativePath, YEAR_MONTH_FORMAT);
		}
	},
	/**
	 * <pre>
	 * 以月份进行存储，不按年份进行分存，如：
	 * /upload/201201
	 * /upload/201312
	 * </pre>
	 */
	MONTH {
		private final SimpleDateFormat MONTH_FORMAT = new SimpleDateFormat("yyyyMM");
		@Override
		public String formatPath(String relativePath) {
			return formatPath(relativePath, MONTH_FORMAT);
		}
	},
	/**
	 * <pre>
	 * 以天进行存储，如：
	 * /upload/2012/1/3
	 * /upload/2013/12/20
	 * </pre>
	 */
	YEAR_MONTH_DAY {
		private final SimpleDateFormat YEAR_MONTH_DAY_FORMAT = new SimpleDateFormat("yyyy" + separatorChar + "MM" + separatorChar + "dd");
		@Override
		public String formatPath(String relativePath) {
			return formatPath(relativePath, YEAR_MONTH_DAY_FORMAT);
		}
	},
	/**
	 * <pre>
	 * 以天进行存储，按月份分存、不按年份进行分存，如：
	 * /upload/201201/4
	 * /upload/201312/22
	 * </pre>
	 */
	MONTH_DAY {
		private final SimpleDateFormat MONTH_DAY_FORMAT = new SimpleDateFormat("yyyyMM" + separatorChar + "dd");
		@Override
		public String formatPath(String relativePath) {
			return formatPath(relativePath, MONTH_DAY_FORMAT);
		}
	},
	/**
	 * <pre>
	 * 以天进行存储，不按月份分存和年份进行分存，如：
	 * /upload/20120104
	 * /upload/20131222
	 * </pre>
	 */
	DAY {
		private final SimpleDateFormat DAY_FORMAT = new SimpleDateFormat("yyyyMMdd");
		@Override
		public String formatPath(String relativePath) {
			return formatPath(relativePath, DAY_FORMAT);
		}
	};
	private static final char separatorChar = '/';
	public abstract String formatPath(String relativePath);

	protected String formatPath(String path, SimpleDateFormat sdf){
		if(path.charAt(path.length() - 1) != separatorChar){
			path += separatorChar;
		}
		return path + sdf.format(new Date());
	}

}
